
package com.j2eefast.flowable.bpm.service;
import cn.hutool.core.util.StrUtil;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.j2eefast.common.core.page.Query;
import com.j2eefast.common.core.utils.PageUtil;
import com.j2eefast.flowable.bpm.entity.ProjectSanLianEntity;
import com.j2eefast.flowable.bpm.mapper.ArchivesMapper;
import com.j2eefast.flowable.bpm.mapper.SanLianMapper;
import com.j2eefast.flowable.bpm.utils.StringUtils;
import com.j2eefast.flowable.bpm.utils.listener.SanlianListener;
import com.j2eefast.framework.annotation.DataFilter;
import com.j2eefast.framework.sys.entity.SysUserEntity;
import com.j2eefast.framework.sys.mapper.SysUserMapper;
import com.j2eefast.framework.utils.Constant;
import com.j2eefast.framework.utils.UserUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p></p>
 *
 * @author: zhangyuan
 * @date: 2021-04-29 23:47
 * @web: http://www.letukeji.cn
 * @version: 1.0.1
 */


@Service
//@Transactional(readOnly = true)
public class SanLianService extends ServiceImpl<SanLianMapper, ProjectSanLianEntity> {

	@Resource
	private SanLianMapper sanLianMapper;
	@Resource
	protected SysUserMapper sysUserMapper;
	@Resource
	protected ArchivesMapper archivesMapper;

	//导入Excel
    public void uploadExcel(MultipartFile file) {
		try {
			EasyExcel.read(file.getInputStream(),ProjectSanLianEntity.class, new SanlianListener(sanLianMapper)).sheet().doRead();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

    //查询三联编号是否存在
	public int getSanlianInfo(String name){

		ProjectSanLianEntity b= sanLianMapper.getSanlianInfo(name);
		if(b!=null){
			return 1;
		}else
			return 0;
	}

	//获取三联最大编号
	public ProjectSanLianEntity getSanlianId(){
		return sanLianMapper.getSanlianId();
	}
	//三联列表
	public PageUtil sanlianList(Map<String, Object> params) {
		Long userId= UserUtils.getUserInfo().getId();
		String status=(String) params.get("status");
		//Integer status = (int)params.get("status");	//汇款单位
		String fileNumber = (String) params.get("fileNumber");
		String sanlianNo = (String) params.get("sanlianNo");
		Page<ProjectSanLianEntity> page = sanLianMapper.sanLianList(	new Query<ProjectSanLianEntity>(params).getPage(),
				userId,
				StrUtil.nullToDefault(status,""),
				StrUtil.nullToDefault(fileNumber,""),
				StrUtil.nullToDefault(sanlianNo,""),
				(String) params.get(Constant.SQL_FILTER));
		return new PageUtil(page);
	}

	public List<ProjectSanLianEntity> sanlianList2(Map<String, Object> params) {
		Long userId= UserUtils.getUserInfo().getId();
		String status=(String) params.get("status");
		String fileNumber = (String) params.get("fileNumber");
		String sanlianNo = (String) params.get("sanlianNo");
		List<ProjectSanLianEntity> list = sanLianMapper.sanLianList2(	new Query<ProjectSanLianEntity>(params).getPage(),
				userId,
				StrUtil.nullToDefault(status,""),
				StrUtil.nullToDefault(fileNumber,""),
				StrUtil.nullToDefault(sanlianNo,""),
				(String) params.get(Constant.SQL_FILTER));
		return list;
	}


	//根据id获取三联信息
	public ProjectSanLianEntity getSanlianById(Long sanlianId){
		return sanLianMapper.getSanlianById(sanlianId);
	}

	//删除
	public boolean deleteSanlian(Long[] ids){
		return sanLianMapper.deleteSanlian(ids);
	}

	/**
	 * 到账认领信息更新
	 *
	 */
	public boolean updateByIds(ProjectSanLianEntity taskFrom){
		return sanLianMapper.updateByIds(taskFrom);
	}

	/**
	 * 添加到账认领信息
	 *
	 */
	public boolean save(ProjectSanLianEntity taskFrom){
		Long userId= UserUtils.getUserInfo().getId();
		taskFrom.setUserId(userId);
		//获取项目经理名字
//		SysUserEntity userEntity = sysUserMapper.getSysById(taskFrom.getNameId());
//		taskFrom.setCeoName(userEntity.getName());
		//查询三联编号
		ProjectSanLianEntity sanlianEntity = sanLianMapper.getSanlianId();
		String code=sanlianCode(sanlianEntity.getName());
		taskFrom.setName(code);

//		String userName=UserUtils.getUserInfo().getName();
//		pmsSampleOutputEntity.setCreateBy(userName);

//		ProjectArchivesEntity fileEntity = archivesMapper.getArchivesById(taskFrom.getFileId());
//		if(fileEntity!=null){
//			taskFrom.setFileName(fileEntity.getName());
//		}
		//测试数据
//		if(taskFrom.getBilling()==null){
//			this.data(taskFrom);
//		}
		return sanLianMapper.save(taskFrom);

	}



//	/**
//	 * 查询用户名称
//	 *
//	 */
//	@DataSource(name = "master")
//	public SysUserEntity getSysById(Long userId){
//		return SanLianMapper.getSysById(userId);
//	}

	/**
	 * 生成编号
	 * @param s
	 * @return
	 */
	public String sanlianCode(String s) {
//		int id = ParamUtils.getInt(s, "-", 0);
		// 对当前日期进行格式化
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmSS");
		String date = sdf.format(new Date());
		//DecimalFormat dft = new DecimalFormat(FORMAT_CODE);
		StringUtils ParamUtils = new StringUtils();

		if (s != "" && s != null) {
			String[] str = s.split("-");
			int[] num = new int[str.length];
			for (int i = 0; i < num.length; i++) {
				num[i] = Integer.parseInt(str[i]);
			}
			int i = num[num.length - 1];
			StringBuilder builder = new StringBuilder();
			builder.append("SL-");
			builder.append(date);
			builder.append("-");
			builder.append(i + 1);
			//String code = ParamUtils.getConteactNo(date, i);
			return builder.toString();
		} else {
			int num = 1;
			StringBuilder builder = new StringBuilder();
			builder.append("SL-");
			builder.append(date);
			builder.append("-");
			builder.append(num);
			//String code = ParamUtils.getConteactNo(date, i);
			return builder.toString();
		}


	}




	@DataFilter(compAlias="c",deptAlias = "d",userAlias = "u")
	public  List<ProjectSanLianEntity> findList(Map<String, Object> params) {
		String name = (String) params.get("name");
		String ceoName = (String) params.get("ceoName");
		String paymentNuit = (String) params.get("paymentNuit");
		String contractSum = (String) params.get("contractSum");
		String projectName = (String) params.get("projectName");
		Long[] ids  = (Long[]) params.get("ids");
		return sanLianMapper.findList(StrUtil.nullToDefault(name,""),
				StrUtil.nullToDefault(ceoName,""),
				StrUtil.nullToDefault(paymentNuit,""),
				StrUtil.nullToDefault(projectName,""),
				StrUtil.nullToDefault(contractSum,""),
				ids,
				(String) params.get(Constant.SQL_FILTER));
	}

	}
